package org.thotheolh.sweatkeeper.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.thotheolh.sweatkeeper.helper.Equipment;
import org.thotheolh.sweatkeeper.util.Resource;

public class NewEquipmentDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField equipNameTf;
	private JTextArea commentsTa;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NewEquipmentDialog dialog = new NewEquipmentDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public NewEquipmentDialog() {
		setTitle("New Equipment");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[][grow]"));
		{
			JLabel equipNameLbl = new JLabel("Equipment Name: ");
			contentPanel.add(equipNameLbl, "cell 0 0,alignx trailing");
		}
		{
			equipNameTf = new JTextField();
			contentPanel.add(equipNameTf, "cell 1 0");
			equipNameTf.setColumns(20);
		}
		{
			JLabel lblComments = new JLabel("Comments: ");
			contentPanel.add(lblComments, "cell 0 1,alignx right,aligny top");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "cell 1 1,grow");
			{
				commentsTa = new JTextArea();
				commentsTa.setWrapStyleWord(true);
				commentsTa.setLineWrap(true);
				scrollPane.setViewportView(commentsTa);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
			{
				JButton okButton = new JButton("Create Equipment");
				okButton.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent evt){
						createEquipment();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}
	
	public void createEquipment(){
		String eName = equipNameTf.getText();
		String eComments = commentsTa.getText();
		Equipment eq = new Equipment();
		eq.setEqName(eName);
		eq.setEqComments(eComments);
		if (eName.isEmpty()){
			JOptionPane.showMessageDialog(null, "Your equipment requires a name !", "Missing Field(s)", JOptionPane.WARNING_MESSAGE);
		} else {
			if (Resource.getInstance().getSql().addEquipment(eq)){
				Resource.getInstance().callRefreshable();
				this.dispose();
				JOptionPane.showMessageDialog(null, "Your equipment has successfully been added !", "Equipment Successfully Added", JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "Failed to add new equipment !/nPlease try again.", "Equipment Add Failed", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
